<?php
function subaccount_del ()
{
    global $gametype;
    global $curruser;
    if ($curruser['account'] != G_SUPERACCOUNT)
    {
        $sqlAdd = " and prev_id = " . $curruser['id'];
    }
    $sqlAdd = "";
    $id = intval($_GET['id']);
    $rs = getdb("select * from k_user where acl=255 and id=" . $id . $sqlAdd);
    if ($rs->eof)
    {
        echo "<p class=error>错误的帐号 ID!</p>";
        exit();
    }
    $account = $rs->f['account'];
    $rsx = getdb("select count(id) from k_user where acl=5 and supe_account='" . $account . "'");
    if (isset($_GET['ok']))
    {
        if (0 < $rsx->f[0])
        {
            getdb("update k_user set supe_account ='' where acl=5 and supe_account='" . $account . "'");
            getdb("delete from amounts where user_id=" . $id);
        }
        getdb("update k_user set enabled=0 where acl=255 and id=" . $id);
        echo "<p>删除帐号成功！</p>";
        exit();
    }
    else
    {
        if (0 < $rsx->f[0])
        {
            echo "<p>是否确定删除这个总监帐号？<b>" . $rs->f['account'] . "</b><br><font color=red>(注意，删除後底下大股东之归属将会被清除！)</font></p>";
        }
        else
        {
            echo "<p>是否确定删除这个公司帐号？<b>" . $rs->f['account'] . "</b></p>";
        }
        echo "<p>[ &nbsp; ";
        echo "<a href=op.php?op=admin&fp=subaccount&gametype=" . $gametype . "&cmd=del&id=" . $id . "&ok=1>是</a>";
        echo " &nbsp; | &nbsp; <a href=op.php?op=admin&fp=subaccount&gametype=" . $gametype . ">否</a> &nbsp; ]</p>";
    }
}

function subaccount_list ()
{
    global $curruser;
    global $gametype;
    $conf = new Config();
    $showCrtFlag = 0;
    $psen_edit_mod = 1;
    $rs = getdb("select id,open from lotto where open>='" . date("Y-m-d") . "' order by open");
    $lotto_id = $rs->f['id'];
    $open = $rs->f['open'];
    $rs->close();
    $stopbet = $conf->get("stopbet");
    $stopbet += 5;
    if (date("Y-m-d") == $open && ( $curruser['account'] != G_SUPERACCOUNT && $curruser['acl'] != 255 ) && $conf->get("startbet") <= date("Hi") && date("Hi") <= $stopbet)
    {
        $psen_edit_mod = 0;
    }
    if ($curruser['account'] == G_SUPERACCOUNT && $curruser['acl'] == 255)
    {
        $psen_edit_mod = 1;
        $showCrtFlag = 1;
    }
    echo "<table border=1 cellpadding=3 cellspacing=0>";
    echo "<tr class='td0 bgClc_{$gametype}'><td nowrap>帐号</td><td nowrap>名称</td><td nowrap align=center>身份</td><td nowrap align=center>查看清单</td><td nowrap align=center>操盘</td><td nowrap align=center>帐号</td><td nowrap align=center>开奖</td><td nowrap align=center>系统</td><td nowrap align=center>功能</td>";
    if ($showCrtFlag)
    {
        echo "<td nowrap>归属</td>";
    }
    echo "</tr>";
    if ($curruser['account'] == G_SUPERACCOUNT && $curruser['acl'] == 255)
    {
        $rs = getdb("select a.*, b.account as Baccount from k_user a LEFT join k_user b on a.prev_id = b.id where a.acl=255 and a.prev_id>0");
    }
    else
    {
        $rs = getdb("select * from k_user where acl=255 and prev_id=" . $curruser['id']);
    }
    while (!$rs->eof)
    {
        $Arr_list = array();
        $subAccRight_Arr = array("view", "control", "account", "open", "system");
        $account = $rs->f['account'];
        $detailed_list = $rs->f['supe_account'];
        $field_manage_power = $rs->f['manage_power'];
        $field_detailed_list = $rs->f['detailed_list'];
        if ($field_detailed_list || $field_manage_power)
        {
            $Arr_list = array($field_detailed_list, 0, $field_manage_power, 0, 0);
        }
        else
        {
            if (strlen($detailed_list) == 5)
            {
                $len = strlen($detailed_list);
                $i = 0;
                while ($i < $len)
                {
                    $Arr_list[$i] = substr($detailed_list, $i, 1);
                    ++$i;
                }
            }
            $Arr_list = array(0, 0, 0, 0, 0);
        }
        $subAccRight_Arr = array_combine($subAccRight_Arr, $Arr_list);
        if (@( $subAccRight_Arr['view'] == 0 ))
        {
            @$subAccRight_view = "×";
        }
        else
        {
            @$subAccRight_view = "○";
        }
        if (@( $subAccRight_Arr['control'] == 0 ))
        {
            @$subAccRight_control = "×";
        }
        else
        {
            @$subAccRight_control = "○";
        }
        if (@( $subAccRight_Arr['account'] == 0 ))
        {
            @$subAccRight_account = "×";
        }
        else if ($subAccRight_Arr['account'] == 1)
        {
            @$subAccRight_account = "○";
        }
        else
        {
            @$subAccRight_account = "v";
        }
        if (@( $subAccRight_Arr['open'] == 0 ))
        {
            @$subAccRight_open = "×";
        }
        else if ($subAccRight_Arr['open'] == 1)
        {
            @$subAccRight_open = "○";
        }
        else
        {
            @$subAccRight_open = "v";
        }
        if (@( $subAccRight_Arr['system'] == 0 ))
        {
            @$subAccRight_system = "×";
        }
        else if ($subAccRight_Arr['system'] == 1)
        {
            @$subAccRight_system = "○";
        }
        else
        {
            @$subAccRight_system = "v";
        }
        echo "<tr>";
        echo "<td nowrap>" . $account . "</td>";
        echo "<td nowrap>" . $rs->f['name'] . "</td>";
        $rsx = getdb("select count(id) from k_user where acl=5 and supe_account='" . $account . "'");
        if (0 < $rsx->f[0])
        {
            echo "<td align=center><font color=green>总监</font></td>";
        }
        else
        {
            echo "<td align=center>公司</td>";
        }
        echo "<td align=center>" . $subAccRight_view . "</td>";
        echo "<td align=center>" . $subAccRight_control . "</td>";
        echo "<td align=center>" . $subAccRight_account . "</td>";
        echo "<td align=center>" . $subAccRight_open . "</td>";
        echo "<td align=center>" . $subAccRight_system . "</td>";
        echo "<td align=center>";
        echo " [ <a href=op.php?op=admin&fp=subaccount&gametype=" . $gametype . "&cmd=edit&id=" . $rs->f['id'] . ">修改</a>";
        if ($psen_edit_mod == 1 && 0 < $rsx->f[0])
        {
            echo " | <a href=op.php?op=admin&fp=amount_3d&gametype=" . $gametype . "&id=" . $rs->f['id'] . ">设定</a>";
        }
        if ($psen_edit_mod == 1 || $rsx->f[0] == 0)
        {
            echo " | <a href=op.php?op=admin&fp=subaccount&gametype=" . $gametype . "&cmd=del&id=" . $rs->f['id'] . ">删除</a>";
        }
        echo " ] </td>";
        if ($showCrtFlag)
        {
            $len = 3 < strlen($rs->f['Baccount']) ? 3 : 1;
            echo "<td nowrap>" . substr($rs->f['Baccount'], 0, $len) . "***" . substr($rs->f['Baccount'], 0 - 1);
        }
        echo "</tr>";
        $rs->movenext();
    }
    echo "</table>";
}

function subaccount_add ()
{
    global $curruser;
    global $gametype;
    $err = "";
    if (isset($_POST['account']))
    {
        $account = getvar("account");
        $pass = getvar("pass");
        if ($account == "")
        {
            $err .= "请输入帐号！\\n";
        }
        if ($pass == "")
        {
            $err .= "请输入密码！\\n";
        }
        if ($err == "")
        {
            $rs = getdb("select * from k_user where account='" . $account . "'");
            if (!$rs->eof)
            {
                $err .= "帐号已经存在，请用其他的帐号！\\n";
            }
        }
        if ($err == "")
        {
            if (!check_account_legal($account))
            {
                $err .= "帐号必须为６个以上的英文字母及数字混和所组成！（请注意英文大小写）";
            }
            if (!check_account_legal($pass) && $err == "")
            {
                $err .= "密码必须为６个以上的英文字母及数字混和所组成！（请注意英文大小写）";
            }
            $field_chk = db_IsExistsField("k_user", "createAccount");
            if ($field_chk === false)
            {
                $err = "资料表异常,请洽系统管理员";
            }
            else if ($field_chk == 0)
            {
                getdb("ALTER TABLE `k_user` ADD `createAccount` VARCHAR( 20 ) NULL ");
            }
            if ($err == "")
            {
                $name = getvar("name");
                if ($name == "")
                {
                    $name = $account;
                }
                $sql = "insert into k_user (account,name,pass,acl,enabled,prev_id,createAccount) values (" . sqlstr($account) . "," . sqlstr($name) . ",'" . md5($pass) . "',255,1," . $curruser['id'] . ",'" . $curruser['account'] . "')";
                getdb($sql);
                echo "<p>新增帐号成功！</p>";
                echo "<p>帐号：<b>{$account}</b><br>";
                echo "密码：<b>{$pass}</b></p>";
            }
        }
    }
    else
    {
        echo "<form action=\"op.php?op=admin&fp=subaccount&gametype=" . $gametype . "&cmd=add\" method=post onsubmit=\"if (this.account.value=='') {\r\n\talert('请输入帐号！');\r\n\tthis.account.focus();\r\n\treturn false;\r\n\t}\r\n\tif (this.pass.value=='') {\r\n\talert('请输入密码！');\r\n\tthis.pass.focus();\r\n\treturn false;\r\n\t}\r\n\tif (this.pass.value != this.pass2.value) {\r\n\talert('两个密码不一样，请检查！');\r\n\tthis.pass.focus();\r\n\treturn false;\r\n\t}\">";
        echo "<table border=0 cellpadding=2 cellspacing=0>";
        echo "<tr><td align=right>帐号：</td><td><input name=account size=20 maxlength=25> (最少6码)</td></tr>";
        echo "<tr><td align=right>名称：</td><td><input name=name size=20 maxlength=25></td></tr>";
        echo "<tr><td align=right>密码：</td><td><input type=password name=pass size=20 maxlength=25> (最少6码)</td></tr>";
        echo "<tr><td align=right>再次输入密码：</td><td><input type=password name=pass2 size=20 maxlength=25></td></tr>";
        echo "</table>";
        echo "<input type=submit value=新增>";
        echo "</form>";
        if ($err != "")
        {
            echo "<script>alert(\"" . $err . "\");</script>";
        }
    }
}

function subaccount_edit ()
{
    global $curruser;
    global $gametype;
    if ($curruser['account'] != G_SUPERACCOUNT)
    {
        $sqlAdd = " and prev_id = " . $curruser['id'];
    }
    else
    {
        $sqlAdd = "";
    }
    $id = intval($_GET['id']);
    $rs = getdb("select * from k_user where acl=255 and id=" . $id . $sqlAdd);
    if ($rs->eof)
    {
        echo "<p class=error>错误的帐号 ID!</p>";
        exit();
    }
    $account = $rs->f['account'];
    $field_manage_power = $rs->f['manage_power'];
    $field_detailed_list = $rs->f['detailed_list'];
    $Arr_list = array();
    $subAccRight_Arr = array("view", "control", "account", "open", "system");
    if ($field_detailed_list || $field_manage_power)
    {
        $Arr_list = array($field_detailed_list, 0, $field_manage_power, 0, 0);
    }
    else
    {
        $detailed_list = $rs->f['supe_account'];
        if (strlen($detailed_list) == 5)
        {
            $len = strlen($detailed_list);
            $i = 0;
            while ($i < $len)
            {
                $Arr_list[$i] = substr($detailed_list, $i, 1);
                ++$i;
            }
        }
        //$Arr_list = array(0, 0, 0, 0, 0);
    }
	
    $subAccRight_Arr = array_combine($subAccRight_Arr, $Arr_list);
    if (@( $subAccRight_Arr['view'] == 0 ))
    {
        $view_pow = "checked";
    }
    else
    {
        $view_pow2 = "checked";
    }
    if (@( $subAccRight_Arr['control'] == 0 ))
    {
        $control_pow = "checked";
    }
    else
    {
        $control_pow2 = "checked";
    }
    if (@( $subAccRight_Arr['account'] == 0 ))
    {
        $account_pow = "checked";
    }
    else if ($subAccRight_Arr['account'] == 1)
    {
        $account_pow2 = "checked";
    }
    else
    {
        $account_pow3 = "checked";
    }
    if (@( $subAccRight_Arr['open'] == 0 ))
    {
        $open_pow = "checked";
    }
    else if ($subAccRight_Arr['open'] == 1)
    {
        $open_pow2 = "checked";
    }
    else
    {
        $open_pow3 = "checked";
    }
    if (@( $subAccRight_Arr['system'] == 0 ))
    {
        $system_pow = "checked";
    }
    else if ($subAccRight_Arr['system'] == 1)
    {
        $system_pow2 = "checked";
    }
    else
    {
        $system_pow3 = "checked";
    }
    $supeacc = 0;
    $rsx = getdb("select id from k_user where acl=5 and supe_account='" . $account . "'");
    if (!$rsx->eof)
    {
        $supeacc = 1;
    }
    $rsx->close();
    $err = "";
    if (ispost())
    {
        $f = array();
        if (getvar("pass") != "")
        {
            if (!check_account_legal(getvar("pass")))
            {
                echo "<p class=error>密码必须为６个以上的英文字母及数字混和所组成！（请注意英文大小写）</p>";
            }
            else
            {
                $f['pass'] = "'" . md5(getvar("pass")) . "'";
            }
        }
        $f['name'] = "'" . getvar("name") . "'";
        $f['manage_power'] = "0";
        $f['detailed_list'] = "0";
        $f['supe_account'] = "'" . getvar("view_pow") . getvar("control_pow") . getvar("account_pow") . getvar("open_pow") . getvar("system_pow") . "'";
        db_update("k_user", $f, "acl=255 and id=" . $id);
        echo "<p>资料修改成功！</p>";
        exit();
    }
    echo "<form action=\"op.php?op=admin&fp=subaccount&gametype=" . $gametype . "&cmd=edit&id=" . $id . "\" method=post onsubmit=\"if (this.pass.value!='') {\r\n\tif (this.pass.value != this.pass2.value) {\r\n\talert('两个密码不一样，请检查！');\r\n\tthis.pass.focus();\r\n\treturn false;\r\n\t}\r\n\t}\">";
    echo "<table border=0 cellpadding=2 cellspacing=0>";
    echo "<tr><td align=right>帐号：</td><td>" . $rs->f['account'] . "</td></tr>";
    echo "<tr><td align=right>名称：</td><td><input name=name value=\"" . $rs->f['name'] . "\" size=20 maxlength=25></td></tr>";
    echo "<tr><td align=right>密码：</td><td><input type=password name=pass size=20 maxlength=25> (最少6码)</td></tr>";
    echo "<tr><td align=right>再次输入密码：</td><td><input type=password name=pass2 size=20 maxlength=25></td></tr>";
    if ($supeacc == 0)
    {
        echo "<tr><td align=right>查看清单权限：</td><td>\r\n            <input type=radio name=view_pow " . @$view_pow . " value=\"0\">无\r\n            <input type=radio name=view_pow " . @$view_pow2 . " value=\"1\">有\r\n          </td></tr>";
    }
    echo "<tr><td align=right>操盘权限：</td><td>\r\n          <input type=radio name=control_pow " . @$control_pow . " value=\"0\">无\r\n          <input type=radio name=control_pow " . @$control_pow2 . " value=\"1\">有\r\n        </td></tr>";
    echo "<tr><td align=right>帐号权限：</td><td>\r\n          <input type=radio name=account_pow " . @$account_pow . " value=\"0\">无\r\n          <input type=radio name=account_pow " . @$account_pow2 . " value=\"1\">唯读\r\n          <input type=radio name=account_pow " . @$account_pow3 . " value=\"2\">可异动\r\n        </td></tr>";
    echo "<tr><td align=right>开奖权限：</td><td>\r\n          <input type=radio name=open_pow " . @$open_pow . " value=\"0\">无\r\n          <input type=radio name=open_pow " . @$open_pow2 . " value=\"1\">唯读\r\n          <input type=radio name=open_pow " . @$open_pow3 . " value=\"2\">可异动\r\n        </td></tr>";
    echo "<tr><td align=right>系统权限：</td><td>\r\n          <input type=radio name=system_pow " . @$system_pow . " value=\"0\">无\r\n          <input type=radio name=system_pow " . @$system_pow2 . " value=\"1\">基本选单\r\n          <input type=radio name=system_pow " . @$system_pow3 . " value=\"2\">系统选单\r\n        </td></tr>";
    echo "</table>";
    echo "<input type=submit value=修改>";
    echo "</form>";
    if ($err != "")
    {
        echo "<script>alert(\"" . $err . "\");</script>";
    }
}

$cmd = "list";
if (isset($_GET['cmd']))
{
    $cmd = $_GET['cmd'];
}
$gametype = intval(getvar("gametype"));
$gType = chkGametype($gametype);
if ($gType == 0)
{
    $ui = new UI();
}
else if (isset($g_menu_enabled[$gametype]) && $g_menu_enabled[$gametype])
{
    include_once( "class_3d/CheckMode_3d.php" );
    $chk = new CheckMode_3d($curruser['acl'], 1, $gametype);
    $chk->checkShow3dPage();
    unset($chk);
    $ui = new UI_3D();
    $jslink = "<script language=\"JScript.Encode\" src=\"site.js\"></script>" . ( "<script language=\"VBScript.Encode\" src=\"site.vbs\"></script>" );
    $ui->set("jslink", $jslink);
    $ui->header();
}
else
{
    exit("error gametype");
}
echo title("公司∣总监帐号管理", "<a href=op.php?op=admin&fp=subaccount&gametype=" . $gametype . "&cmd=list>列表</a>" . " | <a href=op.php?op=admin&fp=subaccount&gametype=" . $gametype . "&cmd=add>新增</a>");
echo "<p>";
switch ($cmd)
{
    case "edit" :
        subaccount_edit();
        break;
    case "add" :
        subaccount_add();
        break;
    case "del" :
        subaccount_del();
        break;
    case "list" :
        subaccount_list();
        break;
}
$ui->footer();
?>